<?php
class apiAction extends baseAction{

	public function check(){
		
		$check = D("ju_check_in");
		$userName = $GLOBALS['userName'];
		$zeroTime = date("Y-m-d")." 00:00:00";
		//echo date('Y-m-d H:i:s','1354348513');
		//echo "<br />";
		//echo date('Y-m-d H:i:s','1354348674');
		if($userName==""){
			$data['msg'] = 'please login first';
			echo $_GET['callback']."(".json_encode($data).")";
			return;
		}
		$checked = $check->where("time>".strtotime($zeroTime)." and time<".time()." and user_name='".$userName."'")->find();
		//echo $check->getLastSql();
		if($checked){
			$data = array("msg"=>"You have checked today");
			
		}else{
			$check->add(array("user_name"=>$userName,"time"=>time()));
			$check->execute("update ju_users set points=points+1 where user_name='".$userName."'");
			$points = D("ju_users")->where("user_name='".$userName."'")->find();
			$data =array(
				"msg"=>"success",
				"code"=>1
			);
			
		}
		$points = D("ju_users")->where("user_name='".$userName."'")->find();
		$data['points'] = $points['points'];
		$data['userName'] = $userName;
		echo $_GET['callback']."(".json_encode($data).")";
	}
	function like(){
		if ($_GET['item_id']==""){
			$this->ajaxReturn(array("error"));
		}
		$userName = $GLOBALS['userName'];
		if($userName==""){
			$data['msg'] = 'please login first';
			$data['code']=0;
			echo $_GET['callback']."(".json_encode($data).")";
			return;
		}
		//status 0 __错误
		//status 1 __成功
		//status 2 __成功但是没有加分；
		//status 3 __已经喜欢过了；
		$data = array(
				'status'=>0,
				'msg'=>'error'
		);
		$user = D("ju_users");
		$like = D("ju_like");
			
		
		$zeroTime = date("Y-m-d")." 00:00:00";
		
		$isLiked = $like->where("user_id='".$userName."' and item_id='".$_GET['item_id']."'")->select();
		
		//echo $like->getLastSql();
		//print_r($isLiked);
		
		if($isLiked[0]['id']==''){//没喜欢过
			$data = array(
				'item_id'=>$_GET['item_id'],
				'user_id' =>$userName,
				'time'=>time(),
				'status'=>1,
				'msg'=>'success'
			);
			$like->add($data);
			
			$liketimes = $like->where("user_id='".$userName."' and time>'".strtotime($zeroTime)."' and time<'".time()."'")->count($sql);
			
			//echo $like->getLastSql();
			
			if($liketimes <5){//每天5分
				$user->execute("update ju_users set points=points+1 where user_name='".$userName."'");
				//)addPoints($userName);
			}else{
				$data["msg"] = 'success,but not add points';
				$data['status'] = 2;
			}
		}else{
			$data["msg"] = 'You have like it before!';
			$data['status'] = 3;
		}
	
		$points = $user->where("user_name='".$userName."'")->select();
		$data['points'] = $points[0]['points'];
		
		echo $_GET['callback']."(".json_encode($data).")";
	}
	
	
	function getComment(){
		$url = "http://comment.jianghu.taobao.com/json/cc_list.htm?_input_charset=utf-8&moreurl=http%3A%2F%2Fju.uz.taobao.com&page_size=10&canFwd=2&view=list_trunPage&isJsonp=true";
		$url .= "&target_key=".$_GET['target_key']."&type_id=1100061&rec_user_id=-1&callback=";
		//echo $url;
		$c = $fetchService->fetchByGet($url);
		
		$c = json_decode($c);
		$c = (array)$c;
		$html = (array)($c['result']);
		$html = $html['html'];
		
		$comment =	split("<li>",$html);
		$first = $comment[1];//第一条评论
		//print_r($first);
			
		$atvar = split("src=\"",$first);
		$atvar1 = split("\"",$atvar[1]);
		$atvar = $atvar1[0];

		$msg0 = split("</a>",$first);		
		$msg1 = $msg0[2];
		$msg2 = split("</p>",$msg1);
		$msg = $msg2[0];
		

		$userName0 = split("@",$first);
		$userName1 = split(":\"",$userName0[1]);
		$userName = $userName1[0];		
		
		$userName = iconv('gb2312','utf-8',$userName);
		$msg = iconv('gb2312','utf-8',$msg);
		if($userName==''){
			$result =0;
		}else{
			$result = 1;
		}
		//echo json_encode($userName);
		$returndata = "<img src=".$atvar.">".$userName.":".$msg;
		//$returndata = base64_encode($returndata);
		$return = array(
				'msg'=>$msg,
				'atvar'=>$atvar,
				'userName'=>$userName
		);
		$return = array(
				'status'=>'1',
				'html'=>$return,
				'msg'=>"",
				'result'=>$result
		);
		echo $_GET['callback']."(".json_encode($return).")";
	}
	
	function categray(){
		$rs = new ju_item();
		$batches = $_GET['batches'];
		if($batches<1)
			$batches = 1;
		$each = 8;
		
		$pageSize = 24;
		$page = $_GET['page'];
		if($page<1)
			$page = 1;
		$sql = "select * from ju_item where classId='".$_GET['classId']."' ";
		$limit = ($page-1) * $pageSize .",". $pageSize;
		$sql .=" limit ".$limit;
		//echo $sql;
		$items = $rs->select($sql);
		
		$returnItems = array();
		for($i=0;$i<8;$i++){
			$returnItems[] = $items[($batches-1)*$each+$i+12]; 
		}
		//print_r($returnItems);
		$return = array(
				"status"=>0,
				"items"=>$returnItems
			);
		echo $_GET['callback']."(".json_encode($return).")";
	}
}
?>